﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConfigMeister
{
    public partial class MaterialBox : Form
    {
        public MaterialBox()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
        }

        private void LoadMaterial()
        {
            if(MaterialBucket.Count == 0) return;

            Material selected = MaterialBucket.Get(listBox1.SelectedItem.ToString());
            nameBox.Text = selected.Name;
            pictureBox1.BackColor = selected.Diffuse;
            pictureBox2.BackColor = selected.Ambient;
            pictureBox3.BackColor = selected.Specular;

            texBox.Text = selected.Texture;

            specularUp.Value = (decimal)selected.specularIntesity;
            GlossBox.Value = selected.Glossiness;
            BlacknessBox.Value = (decimal) selected.ShadedSurfBlacknessCoef;
            comboBox1.SelectedIndex = selected.ShadingAlgorithm;
            checkBox1.Checked = selected.Soften;
            checkBox2.Checked = selected.TwoSided;
        }

        private void Refesh()
        {
            listBox1.Items.Clear();
            listBox1.Items.AddRange(MaterialBucket.GetList());
        }

        private void SaveMaterial()
        {
            if(nameBox.Text == "")
            {
                nameBox.Text = MaterialBucket.GenericName;
            }

            Material temp = new Material
                                {
                                    Name = nameBox.Text,
                                    Diffuse = pictureBox1.BackColor,
                                    Ambient = pictureBox2.BackColor,
                                    Specular = pictureBox3.BackColor,
                                    Texture = texBox.Text,
                                    specularIntesity = (double) specularUp.Value,
                                    Glossiness = (int) GlossBox.Value,
                                    ShadedSurfBlacknessCoef = (double) BlacknessBox.Value,
                                    ShadingAlgorithm = comboBox1.SelectedIndex,
                                    Soften = checkBox1.Checked,
                                    TwoSided = checkBox2.Checked
                                };

            MaterialBucket.Modify(listBox1.SelectedItem.ToString(),temp);
            Refesh();
            listBox1.SelectedItem = temp.Name;
        }

        private void MaterialBox_Load(object sender, EventArgs e)
        {

            listBox1.Items.AddRange(MaterialBucket.GetList());
        }

        private void button8_Click(object sender, EventArgs e)
        {
            SaveMaterial();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            LoadMaterial();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog {Color = pictureBox1.BackColor};
            if(dialog.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.BackColor = dialog.Color;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog { Color = pictureBox2.BackColor };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                pictureBox2.BackColor = dialog.Color;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog { Color = pictureBox3.BackColor };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                pictureBox3.BackColor = dialog.Color;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog
            {
                Title = "Otwórz bitmapę",
                Filter = "Wszystkie pliki (*.*)|*.*|Bitmapy (*.bmp)|*.bmp",
                FilterIndex = 2,
                RestoreDirectory = true
            };

            if(fdlg.ShowDialog() == DialogResult.OK)
            {
                texBox.Text = fdlg.FileName;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Material temp = new Material();


            temp.Name = MaterialBucket.GenericName;

            MaterialBucket.Add(temp);

            Refesh();
            listBox1.SelectedItem = temp.Name;
            LoadMaterial();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listBox1.SelectedIndex == -1)
            {
                groupBox1.Enabled = false;
                return;
            }
            groupBox1.Enabled = true;
            LoadMaterial();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                return;
            }
            MaterialBucket.Remove(listBox1.SelectedItem.ToString());
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
